package com.hx.er.service.manager.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.hx.er.bean.base.FoodBean;
import com.hx.er.bean.base.FoodTemplateBean;
import com.hx.er.bean.base.RestraurantBean;
import com.hx.er.bean.detail.FoodDetailBean;
import com.hx.er.bean.detail.FoodTemplateDetailBean;
import com.hx.er.dao.IFoodDao;
import com.hx.er.dao.IFoodTemplateDao;
import com.hx.er.dao.IRestraurantDao;
import com.hx.er.service.manager.IManagerFoodService;

public class ManagerFoodServiceImpl implements IManagerFoodService {
	@Autowired
	private IFoodDao foodDao;

	@Autowired
	private IFoodTemplateDao foodTemplateDao;

	@Autowired
	private IRestraurantDao restDao;

	@Override
	public List<FoodBean> getFoodAliveList(Map<String, Object> conditions) {
		return this.foodDao.getFoodAliveList(conditions);
	}

	@Override
	public void insertFoodWithoutId(FoodBean foodBean) {
		this.foodDao.insertFoodWithoutId(foodBean);
	}

	@Override
	public int getFoodAliveCount(Map<String, Object> conditions) {
		return this.foodDao.getFoodAliveCount(conditions);
	}

	@Override
	public FoodDetailBean getFoodAliveById(long foodId) {
		return this.foodDao.getFoodAliveById(foodId);
	}

	@Override
	public int deleteFood(FoodBean foodBean) {
		return this.foodDao.deleteFood(foodBean);
	}

	@Override
	public int updateFood(FoodBean foodBean) {
		return this.foodDao.updateFood(foodBean);
	}

	@Override
	public RestraurantBean getRestById(long restId) {
		return this.restDao.getRestById(restId);
	}

	@Override
	public List<FoodTemplateBean> getFoodTemplateAliveList(Map<String, Object> conditions) {
		return this.foodTemplateDao.getFoodTemplateAliveList(conditions);
	}

	@Override
	public FoodTemplateDetailBean getFoodTemplateAliveById(long foodTemplateId) {
		return this.foodTemplateDao.getFoodTemplateAliveById(foodTemplateId);
	}

	@Override
	public int getFoodTemplateAliveCount(Map<String, Object> conditions) {
		return this.foodTemplateDao.getFoodTemplateAliveCount(conditions);
	}

	@Override
	@Transactional
	public boolean foodForward(FoodBean foodBean) {
		try {
			// 获取当前食品和前一个食品
			FoodBean currentFoodBean = this.foodDao.getFoodAliveById(foodBean.getFoodId());
			FoodBean lastFoodBean = this.foodDao.getLastAliveFood(currentFoodBean);

			// 交换位置序号
			long currentSortIndex = currentFoodBean.getSortIndex();
			long lastSortIndex = lastFoodBean.getSortIndex();
			currentFoodBean.setSortIndex(lastSortIndex);
			currentFoodBean.setUpdateUserId(foodBean.getUpdateUserId());
			lastFoodBean.setSortIndex(currentSortIndex);
			lastFoodBean.setUpdateUserId(foodBean.getUpdateUserId());

			// 更新
			this.foodDao.updateFoodSort(currentFoodBean);
			this.foodDao.updateFoodSort(lastFoodBean);

			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	@Transactional
	public boolean foodBackward(FoodBean foodBean) {
		try {
			// 获取当前食品和后一个食品
			FoodBean currentFoodBean = this.foodDao.getFoodAliveById(foodBean.getFoodId());
			FoodBean nextFoodBean = this.foodDao.getNextAliveFood(currentFoodBean);

			// 交换位置序号
			long currentSortIndex = currentFoodBean.getSortIndex();
			long lastSortIndex = nextFoodBean.getSortIndex();
			currentFoodBean.setSortIndex(lastSortIndex);
			currentFoodBean.setUpdateUserId(foodBean.getUpdateUserId());
			nextFoodBean.setSortIndex(currentSortIndex);
			nextFoodBean.setUpdateUserId(foodBean.getUpdateUserId());

			// 更新
			this.foodDao.updateFoodSort(currentFoodBean);
			this.foodDao.updateFoodSort(nextFoodBean);

			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
